package com.avocado.configuration.controller;

import com.avocado.commons.model.http.BaseResponse;
import com.avocado.commons.utils.JsonUtils;
import com.avocado.configuration.dto.SequenceAddDto;
import com.avocado.configuration.dto.SequenceEditDto;
import com.avocado.configuration.dto.SequenceQueryDto;
import com.avocado.configuration.service.SequenceService;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;




@Controller
@RequestMapping("/sequence/*")
public class SequenceController {
	

	 private final SequenceService sequenceService;

    public SequenceController(SequenceService sequenceService) {
        this.sequenceService = sequenceService;
    }


     @RequestMapping("/getOne")
     public @ResponseBody BaseResponse getOne(Long id) {

		return BaseResponse.builder().status(1).data(sequenceService.getOne(id)).build();

     }
	
	 /**
	  * hasAnyAuthority('AUTH_SEQ_ADD')
	  * 表示只有AUTH_SEQ_ADD权限的人才能调用
	  * AUTH_SEQ_ADD在 系统设置/访问控制/功能管理 中的“权限代码”中设置
	  * @param dto
	  * @return
	  * @throws Exception
	  */
	 @PreAuthorize("hasAnyAuthority('SEQ_ADD')")
	 @RequestMapping("/add")
     public @ResponseBody BaseResponse add(@RequestBody @Validated  SequenceAddDto dto) throws Exception{

		 long id= sequenceService.add(dto);

		 return BaseResponse.builder().data(id).message("操作成功!").status(1).build();
     }
	 
	 @PreAuthorize("hasAnyAuthority('SEQ_DELETE')")
	 @RequestMapping("/deleteById")
     public @ResponseBody BaseResponse deleteById(Long id) {

		 int status=sequenceService.deleteById(id)==Boolean.TRUE?1:0;

		 return BaseResponse.builder().message("操作成功!").status(status).build();

     }
	 
	 @PreAuthorize("hasAnyAuthority('SEQ_UPDATE')")
	 @RequestMapping("/update")
     public @ResponseBody BaseResponse update(@RequestBody @Validated  SequenceEditDto dto) throws Exception{

		 sequenceService.update(dto);

		 return BaseResponse.builder().message("操作成功!").status(1).build();
     }
	
	 
	 @PreAuthorize("hasAnyAuthority('SEQ_QUERY')")
	 @RequestMapping("/findByPage")
     public @ResponseBody BaseResponse findByPage(@PageableDefault Pageable pageable, SequenceQueryDto dto) {

		 return BaseResponse.builder().status(1).message("操作成功!").data(sequenceService.findByPage(pageable, dto)).build();

     }
    
	 @RequestMapping("/findAll")
     public @ResponseBody BaseResponse findAll(SequenceQueryDto dto) {

		 return BaseResponse.builder().status(1).data(sequenceService.findAll(dto)).build();
     }
	
	

}
